package com.app.coffeephilosophy.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;

import com.app.coffeephilosophy.R;

public class HandPageViewActivity extends Activity {
	
    private ViewPager viewPager;  
    private ArrayList<View> pageViews;  
    private ViewGroup handpageview, group;  
    private ImageView imageView;  
    private ImageView[] imageViews; 
    private static int b = 0;
    /** Called when the activity is first created. */
     
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        b  = 0;
        LayoutInflater inflater = getLayoutInflater();  
        pageViews = new ArrayList<View>();  
        pageViews.add(inflater.inflate(R.layout.item01, null));  
        pageViews.add(inflater.inflate(R.layout.item02, null));  
        pageViews.add(inflater.inflate(R.layout.item03, null));  
        pageViews.add(inflater.inflate(R.layout.item04, null));   
  
        imageViews = new ImageView[pageViews.size()];  
        handpageview = (ViewGroup)inflater.inflate(R.layout.handpageview, null);  
        
        // group是R.layou.main中的负责包裹小圆点的LinearLayout.  
        group = (ViewGroup)handpageview.findViewById(R.id.viewGroup);  
  
        viewPager = (ViewPager)handpageview.findViewById(R.id.guidePages);  
  
        for (int i = 0; i < pageViews.size(); i++) {  
            imageView = new ImageView(HandPageViewActivity.this);  
            imageView.setLayoutParams(new LayoutParams(20,20));  
            imageView.setPadding(20, 0, 20, 0);  
            imageViews[i] = imageView;  
            if (i == 0) {  
                //默认选中第一张图片
                imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);  
            } else {  
                imageViews[i].setBackgroundResource(R.drawable.page_indicator);  
            }  
            group.addView(imageViews[i]);  
        }  
  
        setContentView(handpageview);  
  
        viewPager.setAdapter(new GuidePageAdapter());  
        viewPager.setOnPageChangeListener(new GuidePageChangeListener());  
    }
     
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		//activity jump
//    	if(pageViews.size()==3){
//		 Intent i=new Intent(HandPageViewActivity.this,CoffeeMainActivity.class);
//		 startActivity(i);
//		 this.finish();
    	
//    	}
		return super.onTouchEvent(event);
	}
    /** 指引页面Adapter */
    class GuidePageAdapter extends PagerAdapter {  
    	  
           
        public int getCount() {  
            return pageViews.size();  
        }  
  
           
        public boolean isViewFromObject(View arg0, Object arg1) {  
            return arg0 == arg1;  
        }  
  
           
        public int getItemPosition(Object object) {  
            // TODO Auto-generated method stub  
            return super.getItemPosition(object);  
        }  
  
           
        public void destroyItem(View arg0, int arg1, Object arg2) {  
            // TODO Auto-generated method stub  
            ((ViewPager) arg0).removeView(pageViews.get(arg1));  
        }  
  
           
        public Object instantiateItem(View arg0, int arg1) {  
            // TODO Auto-generated method stub  
            ((ViewPager) arg0).addView(pageViews.get(arg1));  
            return pageViews.get(arg1);  
        }  
  
           
        public void restoreState(Parcelable arg0, ClassLoader arg1) {  
            // TODO Auto-generated method stub  
  
        }  
  
           
        public Parcelable saveState() {  
            // TODO Auto-generated method stub  
            return null;  
        }  
  
           
        public void startUpdate(View arg0) {  
            // TODO Auto-generated method stub  
  
        }  
  
           
        public void finishUpdate(View arg0) {  
            // TODO Auto-generated method stub  
  
        }  
    } 
    
    /** 指引页面改监听器 */
    class GuidePageChangeListener implements OnPageChangeListener {  
  
           
        public void onPageScrollStateChanged(int arg0) {  
            // TODO Auto-generated method stub  
  
        }  
  
           
        public void onPageScrolled(int arg0, float arg1, int arg2) {  
            // TODO Auto-generated method stub  
  
        }  
  
           
        public void onPageSelected(int arg0) {  
        	 
        	
//         	Toast.makeText(HandPageViewActivity.this, b+"", 0).show();
        	 
            for (int i = 0; i < imageViews.length; i++) {  
                imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused);  
                if (arg0 != i) {  
                    imageViews[i].setBackgroundResource(R.drawable.page_indicator);  
//                   
                }  
                
            }
            
            b++;
            if(b ==3){
            	
           		 Intent i=new Intent(HandPageViewActivity.this,CoffeeMainActivity.class);
           		 startActivity(i);
           		HandPageViewActivity.this.finish();
           
       	    }
  
        }  
  
    }  
    
}